package com.lmc.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lmc.R;
import com.lmc.entity.Health;

import java.util.List;

public class HealthReportAdapter extends BaseAdapter {
    Context context;
    List<Health.DataBean.ResultBean> result;

    public HealthReportAdapter(Context context, List<Health.DataBean.ResultBean> result) {
        this.context = context;
        this.result = result;
    }

    @Override
    public int getCount() {
        return result.size();
    }

    @Override
    public Health.DataBean.ResultBean getItem(int position) {
        return result.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.health_report_list_item, null);
            holder = new ViewHolder();
            holder.name = convertView.findViewById(R.id.name);
            holder.value = convertView.findViewById(R.id.value);
            holder.unit = convertView.findViewById(R.id.unit);
            holder.layout = convertView.findViewById(R.id.layout);
            holder.suggest = convertView.findViewById(R.id.suggest);
            holder.grade_layout_2 = convertView.findViewById(R.id.grade_layout_2);
            holder.layout_2 = convertView.findViewById(R.id.layout_2);
            holder.tv_xy_1 = convertView.findViewById(R.id.tv_xy_1);
            holder.tv_xy_2 = convertView.findViewById(R.id.tv_xy_2);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Health.DataBean.ResultBean bean = getItem(position);
        holder.name.setText(bean.getName());
        holder.value.setText(bean.getValue());
        holder.unit.setText(bean.getUnit());
        holder.suggest.setText(bean.getSuggest());

        holder.layout.removeAllViews();
        holder.layout_2.removeAllViews();
        if (bean.getName().equals("血压")) {
            //血压的需要单独处理
            holder.grade_layout_2.setVisibility(View.VISIBLE);
            holder.tv_xy_1.setVisibility(View.VISIBLE);
            holder.tv_xy_2.setVisibility(View.VISIBLE);
            if (bean.getGrade() != null && bean.getGrade().size() > 0) {
                for (int i = 0; i < bean.getGrade().size(); i++) {
                    Health.DataBean.ResultBean.GradeBean gradeBean = bean.getGrade().get(i);
                    if (!TextUtils.isEmpty(gradeBean.getDiseasestate_name())) {
                        //收缩压
                        View view = View.inflate(context, R.layout.health_list_item_item, null);
                        TextView diseasestate_name = view.findViewById(R.id.diseasestate_name);
                        diseasestate_name.setText(gradeBean.getDiseasestate_name());

                        TextView grade_determin_1 = view.findViewById(R.id.grade_determin_1);
                        TextView grade_determin_2 = view.findViewById(R.id.grade_determin_2);
                        Log.e("----------", "getGrade_determin:" + gradeBean.getGrade_determin());
                        try {
                            String[] sp = gradeBean.getGrade_determin().split("\\|\\|");
                            Log.e("----------", "sp[0]:" + sp[0]);
                            String[] split = sp[0].split("-");
                            if (split.length > 0) {
                                grade_determin_1.setText(split[0]);
                            }
                            if (split.length > 1) {
                                grade_determin_2.setText(split[1]);
                            }
                        } catch (Exception e) {

                        }

                        if (i == 0) {
                            grade_determin_1.setVisibility(View.VISIBLE);
                        } else {
                            grade_determin_1.setVisibility(View.GONE);
                        }

                        View line = view.findViewById(R.id.line);
                        if (bean.getGrade().size() < 3) {
                            switch (gradeBean.getDiseasestate_id()) {
                                case 1:
                                    line.setBackgroundColor(Color.parseColor("#ff0000"));
                                    break;
                                case 2:
                                    line.setBackgroundColor(Color.parseColor("#FF1FCD5C"));
                                    break;
                            }
                        } else if (bean.getGrade().size() == 3) {
                            switch (gradeBean.getDiseasestate_id()) {
                                case 1:
                                    line.setBackgroundColor(Color.parseColor("#ff0000"));
                                    break;
                                case 2:
                                    line.setBackgroundColor(Color.parseColor("#FFF77800"));
                                    break;
                                case 3:
                                    line.setBackgroundColor(Color.parseColor("#FF1FCD5C"));
                                    break;
                            }
                        } else {
                            switch (gradeBean.getDiseasestate_id()) {
                                case 2:
                                case 4:
                                    line.setBackgroundColor(Color.parseColor("#FFF77800"));
                                    break;
                                case 3:
                                    line.setBackgroundColor(Color.parseColor("#FF1FCD5C"));
                                    break;
                                default:
                                    line.setBackgroundColor(Color.parseColor("#ff0000"));
                                    break;
                            }
                        }

                        View round = view.findViewById(R.id.round);
                        if (bean.getDiseasestateid() == gradeBean.getDiseasestate_id()) {
                            round.setVisibility(View.VISIBLE);
                        } else {
                            round.setVisibility(View.INVISIBLE);
                        }
                        holder.layout.addView(view);


                        //舒张压
                        View view2 = View.inflate(context, R.layout.health_list_item_item, null);
                        TextView diseasestate_name2 = view2.findViewById(R.id.diseasestate_name);
                        diseasestate_name2.setText(gradeBean.getDiseasestate_name());

                        TextView grade_determin_2_1 = view2.findViewById(R.id.grade_determin_1);
                        TextView grade_determin_2_2 = view2.findViewById(R.id.grade_determin_2);
                        Log.e("----------", "getGrade_determin:" + gradeBean.getGrade_determin());
                        try {
                            String[] sp = gradeBean.getGrade_determin().split("\\|\\|");
                            Log.e("----------", "sp[1]:" + sp[1]);
                            String[] split = sp[1].split("-");
                            if (split.length > 0) {
                                grade_determin_2_1.setText(split[0]);
                            }
                            if (split.length > 1) {
                                grade_determin_2_2.setText(split[1]);
                            }
                        } catch (Exception e) {

                        }

                        if (i == 0) {
                            grade_determin_2_1.setVisibility(View.VISIBLE);
                        } else {
                            grade_determin_2_1.setVisibility(View.GONE);
                        }

                        View line_2 = view2.findViewById(R.id.line);
                        if (bean.getGrade().size() < 3) {
                            switch (gradeBean.getDiseasestate_id()) {
                                case 1:
                                    line_2.setBackgroundColor(Color.parseColor("#ff0000"));
                                    break;
                                case 2:
                                    line_2.setBackgroundColor(Color.parseColor("#FF1FCD5C"));
                                    break;
                            }
                        } else if (bean.getGrade().size() == 3) {
                            switch (gradeBean.getDiseasestate_id()) {
                                case 1:
                                    line_2.setBackgroundColor(Color.parseColor("#ff0000"));
                                    break;
                                case 2:
                                    line_2.setBackgroundColor(Color.parseColor("#FFF77800"));
                                    break;
                                case 3:
                                    line_2.setBackgroundColor(Color.parseColor("#FF1FCD5C"));
                                    break;
                            }
                        } else {
                            switch (gradeBean.getDiseasestate_id()) {
                                case 2:
                                case 4:
                                    line_2.setBackgroundColor(Color.parseColor("#FFF77800"));
                                    break;
                                case 3:
                                    line_2.setBackgroundColor(Color.parseColor("#FF1FCD5C"));
                                    break;
                                default:
                                    line_2.setBackgroundColor(Color.parseColor("#ff0000"));
                                    break;
                            }
                        }

                        View round_2 = view2.findViewById(R.id.round);
                        if (bean.getDiseasestateid() == gradeBean.getDiseasestate_id()) {
                            round_2.setVisibility(View.VISIBLE);
                        } else {
                            round_2.setVisibility(View.INVISIBLE);
                        }
                        holder.layout_2.addView(view2);
                    }
                }

            }

        } else {
            holder.grade_layout_2.setVisibility(View.GONE);
            holder.tv_xy_1.setVisibility(View.GONE);
            holder.tv_xy_2.setVisibility(View.GONE);
            if (bean.getGrade() != null && bean.getGrade().size() > 0) {

                for (int i = 0; i < bean.getGrade().size(); i++) {
                    Health.DataBean.ResultBean.GradeBean gradeBean = bean.getGrade().get(i);
                    if (!TextUtils.isEmpty(gradeBean.getDiseasestate_name())) {
                        View view = View.inflate(context, R.layout.health_list_item_item, null);
                        TextView diseasestate_name = view.findViewById(R.id.diseasestate_name);
                        diseasestate_name.setText(gradeBean.getDiseasestate_name());

                        TextView grade_determin_1 = view.findViewById(R.id.grade_determin_1);
                        TextView grade_determin_2 = view.findViewById(R.id.grade_determin_2);
                        Log.e("----------", "getGrade_determin:" + gradeBean.getGrade_determin());
                        try {
                            String[] split = gradeBean.getGrade_determin().split("-");
                            if (split.length > 0) {
                                grade_determin_1.setText(split[0]);
                            }
                            if (split.length > 1) {
                                grade_determin_2.setText(split[1]);
                            }
                        } catch (Exception e) {

                        }

                        if (i == 0) {
                            grade_determin_1.setVisibility(View.VISIBLE);
                        } else {
                            grade_determin_1.setVisibility(View.GONE);
                        }

                        View line = view.findViewById(R.id.line);
                        if (bean.getGrade().size() < 3) {
                            switch (gradeBean.getDiseasestate_id()) {
                                case 1:
                                    line.setBackgroundColor(Color.parseColor("#ff0000"));
                                    break;
                                case 2:
                                    line.setBackgroundColor(Color.parseColor("#FF1FCD5C"));
                                    break;
                            }
                        } else if (bean.getGrade().size() == 3) {
                            switch (gradeBean.getDiseasestate_id()) {
                                case 1:
                                    line.setBackgroundColor(Color.parseColor("#ff0000"));
                                    break;
                                case 2:
                                    line.setBackgroundColor(Color.parseColor("#FFF77800"));
                                    break;
                                case 3:
                                    line.setBackgroundColor(Color.parseColor("#FF1FCD5C"));
                                    break;
                            }
                        } else {
                            switch (gradeBean.getDiseasestate_id()) {
                                case 2:
                                case 4:
                                    line.setBackgroundColor(Color.parseColor("#FFF77800"));
                                    break;
                                case 3:
                                    line.setBackgroundColor(Color.parseColor("#FF1FCD5C"));
                                    break;
                                default:
                                    line.setBackgroundColor(Color.parseColor("#ff0000"));
                                    break;
                            }
                        }

                        View round = view.findViewById(R.id.round);
                        if (bean.getDiseasestateid() == gradeBean.getDiseasestate_id()) {
                            round.setVisibility(View.VISIBLE);
                        } else {
                            round.setVisibility(View.INVISIBLE);
                        }
                        holder.layout.addView(view);
                    }
                }

            }
        }


        return convertView;
    }

    private class ViewHolder {
        TextView name;
        TextView value;
        TextView unit;
        LinearLayout layout;
        TextView suggest;
        View grade_layout_2;
        LinearLayout layout_2;
        TextView tv_xy_1, tv_xy_2;

    }
}
